package StackPackage;

/**
 * @author Lzm
 * @version 1.0
 */
public class StackTest {
  public static void main(String[] args) {
    Stack stack = new Stack(4);
    //stack.list();

    System.out.println(stack.isEmpty());

    System.out.println(stack.isFull());

    stack.push(1);
    stack.push(1);
    stack.push(1);
    stack.push(-1);
    System.out.println(stack.isFull());
    stack.list();

    System.out.println(stack.pop());

    System.out.println(stack.isEmpty());

    System.out.println(stack.isFull());

  }
}
/*定义栈*/
class Stack{
  private int top;
  private int[] stack;
  /*构造器*/
  public Stack(int size){
    this.top = -1;
    stack = new int[size];
  }
  /*判栈空*/
  public boolean isEmpty(){
    return top == -1;
  }
  /*判栈满*/
  public boolean isFull(){
    return top == this.stack.length - 1;
  }
  /*入栈*/
  public void push(int number){
      if (isFull()){
        throw new RuntimeException("栈满");
      }
      stack[++top] = number;
  }
  /*出栈*/
  public int pop(){
    if (isEmpty()){
      throw new RuntimeException("栈空");
    }
    return stack[top--];
  }
  /*遍历栈*/
  public void list(){
    if (isEmpty()){
      throw new RuntimeException("栈空");
    }
    for (int i = this.top ; i >= 0 ; i --){
      System.out.println(stack[i]);
    }
  }
}
